#include "MyString.h"
#include <cstring>

MyString::MyString() : size(0), capacity(15)
{
    s = new char(capacity);
}

MyString::MyString(const char *str)
{
    // 考虑问题：如果字符串长度超过了其容量
    size = strlen(str);
    capacity = size + 1 > 15 ? size + 1 : 15;
    s = new char(capacity);
    strcpy(s, str);
}

MyString::~MyString();
{
    delete[] s;
}

// 拷贝构造
MyString::MyString(const MyString &string)
{
    size = string.size;
    capacity = string.capacity;
    s = new char(capacity);
    strcpy(s, string.s);
}

MyString& MyString::operator=(const MyString &string)
{
    delete[] a;
    size = string.size;
    capacity = string.capacity;
    s = new char(capacity);
    strcpy(s, string.s);
    return *this;
}

// 扩容 
void MyString::ReSize(int capacity)
{
    this->capacity = capacity;
    char *newS = new char[capacity];
    size = size > capacity - 1 ? capacity - 1 : size;  // 往小缩或往大扩   
    strncpy(newS, s, size);
    delete[] s;
    s = newS;
}

MyString& MyString::operator+=(const MyString &string)
{
    size += string.size;
    if (size + 1 > capacity)
    {
        ReSize(size + 1);
    }
    strcat(this->s, string.s);
    return *this;
}

MyString MyString::operator+(const MyString &string)
{
    // 是要在自己的身上进行拼接
    // return strcat(this->s,string.s); // 这个是拼接别人的字符串
    MyString result = *this;
    // result = this->s += string.s;  // 不仅仅是字符串要拼接，是整个类都要拼接
    result += string;
    return *this;
}

MyString& MyString::operator+=(const char* c)
{
    if(size + 1 > capacity)
    {
        ReSize(capacity*2);  
    }
    this->s[size++] = c;
    this->s.[size] = '\0';
    return *this;
}

char &MyString operator[](int index)
{
    return this->s.[index];
}

bool MyString& operator==(const MyString &string)
{
    return strcmp(this->s,string.s);
}

bool operator!=(const MyString &string)
{
    return strcmp(this->s,string.s) != 0;
}

MyString& MyString::operator-=(const MyString &string)  // 移除字符串
{
    char *dest = strstr(this->s,string.s);
    while(dest != nullptr)
    {
        char *back = dest + string.size;
        if(*back != '\0')
        {
            *dest++ = *back++;
        }
        *dest = '\0';
        this->size -= string.size;
        dest = strstr(this->s,string.s);
    }
    if(size + 1 < capacity / 2)
    {
        ReSize(capacity / 2);
    }
    return *this;
}

MyString MyString::operator-(const MyString &string)
{
    MyString result = *this;
    result -= string;
    return *this;
}

MyString::operator int()
{
    return atoi(this->s);
}

MyString::operator int()
{
    return atof(this->s);
}

std::ostream& operator<<(std::ostream &os,const MyString& string)
{
    os<<string.s;
    return os;
}

std::istream& operator>>(std::istream &is,const MyString& string)
{
    char c = 0;
    while((c = getchar()) != '\n' && c!= ' ')
    {
        string += c;
    }
    return is;
}

// 带参构造
MyStringArray::MyStringArray(const MyString &string)
{
    if(size == capacity)
    {
        capacity *= 2;
    }
    array = new MyString[capacity];
}